package com.zmj.framework.dao.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zmj.framework.bean.CardDetail;
import com.zmj.framework.bean.CardMoney;
import com.zmj.framework.bean.CardTime;
import com.zmj.framework.dao.CardDao;
@Repository
public class CardDaoJdbc implements CardDao {
	@Autowired
	FrmJdbcTemplate jdbcTemplate;
	
	public int saveCardMoney(CardMoney cardMoney,CardDetail cardDetail) {
		int ret=0;
		CardMoney exist=getCardMoneyByHykh(cardMoney.getHykh());
		if(exist==null){
			ret=jdbcTemplate.update("insert into card_money(HYKH,DQJE,LJJE,KKRQ,GXSJ,ZT) values("  +
					" '"+cardMoney.getHykh()+"', " +
					" "+cardMoney.getSjje()+", " +
					" "+cardMoney.getSjje()+", " +
					" '"+cardMoney.getKkrq()+"', " +
					" now(), "+
					" '1'"+
					")");
		}else{
			ret=jdbcTemplate.update("update card_money set"  +
					" dqje=dqje+"+cardMoney.getSjje()+", " +
					" ljje=ljje+"+cardMoney.getSjje()+", " +
					" gxsj=now()"+
					" where hykh='"+cardMoney.getHykh()+"'");
		}
		ret=jdbcTemplate.update("insert into card_detail(HYKH,JFJE,SJJE,YHJE,XJ,XYK,BKLB,CZY,CZSJ,BZ,JFRQ) values("  +
				" '"+cardDetail.getHykh()+"', " +
				" "+cardDetail.getJfje()+", " +
				" "+cardDetail.getSjje()+", " +
				" "+(cardDetail.getSjje()-cardDetail.getJfje())+", " +
				" "+cardDetail.getXj()+", " +
				" "+cardDetail.getXyk()+", " +
				" '"+"1"+"', " +
				" '"+cardDetail.getCzy()+"', " +
				" now() "+", " +
				" '"+cardDetail.getBz()+"', " +
				" '"+cardDetail.getJfrq()+"' " +
				")");		
		return ret;
	}

	public CardMoney getCardMoneyByHykh(String hykh) {
		String tmpSql = "select ID, HYKH, DQJE, LJJE,DATE_FORMAT(GXSJ,'%Y-%m-%d') KKRQ,DATE_FORMAT(GXSJ,'%Y-%m-%d %H:%i:%S') GXSJ, ZT from card_money where hykh='" + hykh + "'";
		List list = this.jdbcTemplate.queryForList(tmpSql, CardMoney.class);

		if ((list == null) || (list.size() == 0)) {
			return null;
		}
		return ((CardMoney) list.get(0));
	}

	public List<CardDetail> getCardDetailListByHykh(String hykh) {
		String tmpSql = "select id,hykh,jfje,sjje,yhje,xj,xyk,bklb,czy,DATE_FORMAT(czsj,'%Y-%m-%d %H:%i:%S') czsj,bz,DATE_FORMAT(jfrq,'%Y-%m-%d') jfrq from card_detail where hykh='" + hykh + "' order by jfrq";
		List<CardDetail> list = this.jdbcTemplate.queryForList(tmpSql, CardDetail.class);
		return list;
	}

	public CardTime getCardTimeByHykhAndSpbh(String hykh,String spbh) {
		String tmpSql = "select ID, HYKH, SPBH, KKCS, XFCS,DATE_FORMAT(KKRQ,'%Y-%m-%d') KKRQ,DATE_FORMAT(GXSJ,'%Y-%m-%d %H:%i:%S') GXSJ, ZT from card_time where hykh='" + hykh + "' and spbh='"+spbh+"'";
		List list = this.jdbcTemplate.queryForList(tmpSql, CardTime.class);
		if ((list == null) || (list.size() == 0)) {
			return null;
		}
		return ((CardTime) list.get(0));
	}
	
	public int saveCardTime(List<CardTime> cardTimeList,CardDetail cardDetail) {
		int ret=0;
		for(CardTime cardTime:cardTimeList){
			CardTime exist=getCardTimeByHykhAndSpbh(cardTime.getHykh(),cardTime.getSpbh());
			if(exist==null){
				ret=jdbcTemplate.update("insert into card_time(HYKH,SPBH,KKCS,XFCS,KKRQ,GXSJ,ZT) values("  +
						" '"+cardTime.getHykh()+"', " +
						" '"+cardTime.getSpbh()+"', " +
						" "+cardTime.getKkcs()+", " +//KKCS
						" 0, " +//XFCS
						" now(), "+
						" now(), "+
						" '1'"+
						")");
			}else{
				ret=jdbcTemplate.update("update card_time set"  +
						" kkcs=kkcs+"+cardTime.getKkcs()+", " +
						" gxsj=now()"+
						" where hykh='"+cardTime.getHykh()+"' and spbh='"+cardTime.getSpbh()+"'");
			}
		}
		ret=jdbcTemplate.update("insert into card_detail(HYKH,JFJE,SJJE,YHJE,XJ,XYK,BKLB,CZY,CZSJ,BZ,JFRQ) values("  +
				" '"+cardDetail.getHykh()+"', " +
				" "+cardDetail.getJfje()+", " +
				" "+cardDetail.getSjje()+", " +
				" "+(cardDetail.getSjje()-cardDetail.getJfje())+", " +
				" "+cardDetail.getXj()+", " +
				" "+cardDetail.getXyk()+", " +
				" '"+"2"+"', " +
				" '"+cardDetail.getCzy()+"', " +
				" now() "+", " +
				" '"+cardDetail.getBz()+"', " +
				" '"+cardDetail.getJfrq()+"'" +
				")");
		return ret;
	}

	public List<CardTime> getCardTimeListByHykh(String hykh) {
		String tmpSql = "select ID, HYKH, SPBH, KKCS, XFCS,DATE_FORMAT(KKRQ,'%Y-%m-%d') KKRQ,DATE_FORMAT(GXSJ,'%Y-%m-%d %H:%i:%S') GXSJ, ZT from card_time where hykh='" + hykh + "' order by gxsj";
		List<CardTime> list = this.jdbcTemplate.queryForList(tmpSql, CardTime.class);
		return list;
	}

}
